package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.Vehiculo;

public class VehiculoDAOImpl extends JdbcDaoSupport implements IVehiculoDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM GES_VEHICULO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(Vehiculo vehiculo) {
		deleteOperation.execute( vehiculo  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public Vehiculo findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(Vehiculo vehiculo) {
		insertOperation.execute( vehiculo );
	}

	public void update(Vehiculo vehiculo) {
		updateOperation.execute( vehiculo );
	}

	public Vehiculo findByCriteria(Vehiculo  vehiculo) {
		return findByCriteriaQuery.runQuery(vehiculo);
	}

	public List load(Vehiculo vehiculo) {
		return findAllQuery.runQuery(vehiculo);
	}

	public Vehiculo findByActiveState(Vehiculo  vehiculo) {
		return findByActiveStateQuery.runQuery(vehiculo);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_VEHICULO_T ("
				  + " ID, "
				  + " ID_VEHICULO_TIPO, "
				  + " PLACA , "
				  + " MUNICIPIO, "
				  + " MARCA, "
				  + " MODELO, "
				  + " CHASIS, "
				  + " MOVIL, "
				  + " DESCRIPCION, "
				  + " ESTADO, "
				  + " CAPACIDAD, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " GES_VEHICULO_T_SEC.nextVal, "
				  + PARAMETER
				  + " UPPER( ? ) , "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(Vehiculo vehiculo) {
			InsertOperation.this.update( new Object[]{
				//vehiculo.getId(),
				vehiculo.getIdVehiculoTipo(),
				vehiculo.getPlaca(),
				vehiculo.getMunicipio(),
				vehiculo.getMarca(),
				vehiculo.getModelo(),
				vehiculo.getChasis(),
				vehiculo.getMovil(),
				vehiculo.getDescripcion(),
				vehiculo.getEstado(),
				vehiculo.getCapacidad(),
				vehiculo.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_VEHICULO_T SET "
					  + " ID_VEHICULO_TIPO = ? ,"
					  + " PLACA = ? ,"
					  + " MUNICIPIO = ? ,"
					  + " MARCA = ? ,"
					  + " MODELO = ? ,"
					  + " CHASIS = ? ,"
					  + " MOVIL = ? ,"
					  + " DESCRIPCION = ? ,"
					  + " ESTADO = ? ,"
					  + " CAPACIDAD = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
					  + WHERE 
					  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(Vehiculo vehiculo) {
			UpdateOperation.this.update( new Object[]{
				vehiculo.getIdVehiculoTipo(),
				vehiculo.getPlaca(),
				vehiculo.getMunicipio(),
				vehiculo.getMarca(),
				vehiculo.getModelo(),
				vehiculo.getChasis(),
				vehiculo.getMovil(),
				vehiculo.getDescripcion(),
				vehiculo.getEstado(),
				vehiculo.getCapacidad(),
				vehiculo.getUsuarioModifica(),
				vehiculo.getId(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_VEHICULO_T "
										+ WHERE
			  	  	  					+ " PLACA = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(Vehiculo vehiculo) {
			this.update( new Object[]{
				vehiculo.getPlaca(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_VEHICULO_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			Vehiculo vehiculo = new Vehiculo();
				vehiculo.setId(new Long(rs.getLong("ID")));
				vehiculo.setIdVehiculoTipo(new Long(rs.getLong("ID_VEHICULO_TIPO")));
				vehiculo.setPlaca(rs.getString("PLACA"));
				vehiculo.setMunicipio(rs.getString("MUNICIPIO"));
				vehiculo.setMarca(rs.getString("MARCA"));
				vehiculo.setModelo(rs.getString("MODELO"));
				vehiculo.setChasis(rs.getString("CHASIS"));
				vehiculo.setMovil(rs.getString("MOVIL"));
				vehiculo.setDescripcion(rs.getString("DESCRIPCION"));
				vehiculo.setEstado(rs.getString("ESTADO"));
				vehiculo.setCapacidad(rs.getString("CAPACIDAD"));
				vehiculo.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				vehiculo.setFechaCrea(rs.getDate("FECHA_CREA"));		
				vehiculo.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				vehiculo.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return vehiculo;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
											+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public Vehiculo runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (Vehiculo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " AND UPPER(PLACA) LIKE ? "
										+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public Vehiculo runQuery(Vehiculo vehiculo) {
			List result =  execute( new Object[]{
				new String("%" + (vehiculo.getPlaca() != null && !vehiculo.getPlaca().equals("") ? vehiculo.getPlaca().toUpperCase() : "%") + "%")					
			});
			if ( result.size() > 0 ) {
        		return (Vehiculo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE 
										+ "1=1 "
										+ "AND UPPER(PLACA) LIKE UPPER(?) ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter(new SqlParameter(Types.CHAR));
			compile();
    	}
    	public List runQuery(Vehiculo vehiculo) {
			List result =  execute(new Object[]{
					vehiculo.getPlaca()});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public Vehiculo runQuery(Vehiculo vehiculo) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (Vehiculo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
